<?

class ReleaseList extends db {
	function get_releases() {
		$query = 'select ID from Releases order by date desc';
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}
		$re = array();
		while ($row = mysql_fetch_assoc($r)) {
			$re[] = Release::get($row['ID']);
		}
		return $re;
	}
	
	function get_latest_release() {
		$query = 'select ID from Releases where is_active = 1 order by date desc limit 1';
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}
		$re = array();
		while ($row = mysql_fetch_assoc($r)) {
			$re[] = Release::get($row['ID']);
		}
		return $re;
	}
	
	function get_active_releases() {
		$query = 'select ID from Releases where is_active = 1 order by date desc';
		$r = mysql_query($query);
		if (!$r) {
			return Error::MySQL();
		}
		$re = array();
		while ($row = mysql_fetch_assoc($r)) {
			$re[] = Release::get($row['ID']);
		}
		return $re;
	}
}

class Release extends db {

	var $ID, $is_active, $title, $sub_title, $notes, $date, $press_notes, $av_area_id, $photo_area_id;
	
	function getID() { return $this->ID; }
	function getSubTitle() {return $this->sub_title;}
	function isActive() {return $this->is_active;}
	function getTitle() {return $this->title;}
	function getNotes() {return $this->notes;}
	function getPressNotes() {return $this->press_notes;}
	function getDate($dateMask = null) {
		$dateMask = (!$dateMask) ? DATE_FORMAT : $dateMask;
		return date($dateMask, strtotime($this->date));
	}
	function getAVAreaID() {return $this->av_area_id;}
	function getAVAreaObject() {
		if ($this->av_area_id > 0) {
			$ma = MediaArea::get($this->av_area_id);
			return $ma;
		} else {
			$e = new Error();
			$e->add("An administrator has not yet registered this show for audio or videos.");
			return $e;
		}
	}
	function getPhotoAreaID() {return $this->photo_area_id;}
	function getPhotoAreaObject() {
		if ($this->photo_area_id > 0) {
			$ma = MediaArea::get($this->photo_area_id);
			return $ma;
		} else {
			$e = new Error();
			$e->add("An administrator has not yet registered this show for photos.");
			return $e;
		}
	}
	
	function get($releaseID) {
		if (is_numeric($releaseID) && $releaseID > 0) {
			$q = "select * from Releases where ID = " . $releaseID;
			$db = new db;
			$r = mysql_query($q);
			if (!$r) {
				return Error::MySQL();
			}
			
			$row = mysql_fetch_assoc($r);
			if ($row['ID']) {
				$re = new Release;
				$re->ID = $row['ID'];
				$re->title = $db->sanitize_from_db($row['title']);
				$re->sub_title = $db->sanitize_from_db($row['sub_title']);
				$re->notes = $db->sanitize_from_db($row['notes']);
				$re->press_notes = $db->sanitize_from_db($row['press_notes']);
				$re->date = $row['date'];
				$re->is_active = $row['is_active'];
				$re->av_area_id = $row['av_area_id'];
				$re->photo_area_id = $row['photo_area_id'];
				return $re;
			}
		}
		
		return Error::create("Invalid release ID.");
	}
	
	function getReleaseTracks() {
		$q = "select ID from Release_Tracks where release_id = '{$this->ID}' order by number asc";
		$r = mysql_query($q);
		$tracks = array();
		while ($row = mysql_fetch_assoc($r)) {
			$tracks[] = ReleaseTrack::get($row['ID']);
		}		
		return $tracks;
	}
	
	function getReleaseBandMembers() {
		$q = "select member_id from Releases_to_Band_Members where release_id = '{$this->ID}'";
		$r = mysql_query($q);
		$members = array();
		while($row = mysql_fetch_assoc($r)) {
			$members[] = BandMember::get($row['member_id']);
		}		
		return $members;
	}
	
	function selectBandMembers($memberIDArray) {
		if (User::isAdmin()) {
			$q = "delete from Releases_to_Band_Members where release_id = " . $this->ID;
			$r = mysql_query($q);
			if (is_array($memberIDArray)) {
				foreach($memberIDArray as $mID) {
					$bm = BandMember::get($mID);
					if (!db::isError($bm)) {
						$q = "insert into Releases_to_Band_Members (member_id, release_id) values (" . $bm->getID() . "," . $this->ID . ")";
						$r = mysql_query($q);
					}
				}
			}
		} else {
			return Error::create("You may not select the band members for a particular release.");
		}
	}
	
	function selectGuestPerformers($pIDArray) {
		if (User::isAdmin()) {
			$q = "delete from Releases_to_Guest_Performers where release_id = " . $this->ID;
			$r = mysql_query($q);
			if (is_array($pIDArray)) {
				foreach($pIDArray as $gpID) {
					$gp = BandGuestPerformer::get($gpID);
					if (!db::isError($gp)) {
						$q = "insert into Releases_to_Guest_Performers (guest_performer_id, release_id) values (" . $gp->getID() . "," . $this->ID . ")";
						$r = mysql_query($q);
					}
				}
			}
		} else {
			return Error::create("You may not select the guest performers for a particular release.");
		}
	}

	
	function getReleaseGuestPerformers() {
		$q = "select guest_performer_id from Releases_to_Guest_Performers where release_id = '{$this->ID}'";
		$r = mysql_query($q);
		$gps = array();
		while($row = mysql_fetch_assoc($r)) {
			$gps[] = BandGuestPerformer::get($row['guest_performer_id']);
		}		
		return $gps;
	}
	
	function activate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Releases set is_active = 1 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may activate releases.');
		}
	}
	
	function deactivate() {
		if (User::isAdmin()) {
			$r = @mysql_query("update Releases set is_active = 0 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may deactivate releases.');
		}
	}
	
	function remove() {
		if (User::isAdmin()) {
			$r = @mysql_query("delete from Releases where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('Only an administrator may delete releases.');
		}
	}
	
	function update($postArray) { 
		$db = new db;
		if (!User::isAdmin()) {
			return Error::create("You are not allowed to update releases.");
		}
		
		$title = $db->sanitize_to_db($postArray['title']);
		$sub_title = $db->sanitize_to_db($postArray['sub_title']);
		$date = $db->sanitize_to_db($postArray['date']);
		$_dt = strtotime($date);
		$date = date('Y-m-d', $_dt);
				
		$notes = $db->sanitize_to_db($postArray['notes']);
		$press_notes = $db->sanitize_to_db($postArray['press_notes']);
			
		if (!$title) {
			$title = '(untitled release)';
		}
			
		$r = @mysql_query("update Releases set title='$title', sub_title='$sub_title', date='$date', notes = '$notes', press_notes = '$press_notes' where ID = " . $this->ID);
		if ($r) {
			return Release::get($this->ID);
		} else {
			return Error::MySQL();
		}
	}
	
	function add($postArray) {
		$db = new db;
		if (!User::isAdmin()) {
			return Error::create("You are not allowed to add releases.");
		}
		
		$title = $db->sanitize_to_db($postArray['title']);
		$sub_title = $db->sanitize_to_db($postArray['sub_title']);
		$date = $db->sanitize_to_db($postArray['date']);
		$_dt = strtotime($date);
		$date = date('Y-m-d', $_dt);

		$notes = $db->sanitize_to_db($postArray['notes']);
		$press_notes = $db->sanitize_to_db($postArray['press_notes']);
		
		if (!$title) {
			$title = '(untitled release)';
		}
		
		$r = @mysql_query("insert into Releases (title, sub_title, date, notes, press_notes, is_active) values ('$title', '$sub_title', '$date', '$notes', '$press_notes', " . DEFAULT_ACTIVE . ")");	
		if ($r) {
			return Release::get(mysql_insert_id());
		} else {
			return Error::MySQL();
		}
	}
	
	function addTrack($postArray) {
		$db = new db;
		include_class('text');
		if (User::isAdmin()) {
			$title = $db->sanitize_to_db($postArray['title']);
			$number = $db->sanitize_to_db($postArray['number']);
			if ($postArray['length'] != "") {
				$length = $db->sanitize_to_db($postArray['length']);
				$length = "'00:{$length}'";
			} else {
				$length = "null";
			}
			
			if (!Text::isRTELoaded('description')) {
				$postArray['lyrics'] = nl2br($postArray['lyrics']);
			}
			if (!Text::isRTELoaded('description')) {
				$postArray['meta_information'] = nl2br($postArray['meta_information']);
			}
			
			$meta_information = $db->sanitize_to_db($postArray['meta_information']);
			$lyrics = $db->sanitize_to_db($postArray['lyrics']);
			
			if (!$title) {
				$title = '(untitled track)';
			}

			$r = @mysql_query("insert into Release_Tracks (title, number, meta_information, lyrics, length, release_id) values ('$title', '$number', '$meta_information', '$lyrics', {$length}, '{$this->ID}')");
			if (!$r) {
				return Error::MySQL();
			} else {
				$rt = ReleaseTrack::get(mysql_insert_id());
				return $rt;
			}	
		} else {
			return Error::create("You are not allowed to add tracks to a release.");
		}
	}
	
	function getCover() {
		// the album cover is actually the first piece of media that's active in a given release's media area
		// first we get the media area ID for the given release.
		if ($this->photo_area_id) {
			// if we have an area ID, we continue
			$q = "select ID from DarkRoom_Media_to_Areas where area_id = {$this->photo_area_id} and is_active = 1 order by display_order asc limit 1";
			$r = mysql_query($q);
			if (!$r) {
				return Error::MySQL();
			}
			$row = mysql_fetch_assoc($r);
			if ($row['ID']) {
				$mi = MediaInstance::get($row['ID']);
				return $mi;
			}
		}
	}
	
	function registerMedia($mediaType) {
		$type = ($mediaType == 'av') ? 'av' : 'photos';
		if (User::isAdmin()) { 
			if (($type == 'av' && $this->av_area_id < 1) || ($type == 'photos' && $this->photo_area_id < 1)) {
				$areaTitle = ($type == 'av') ? "Release Audio/Video: " : "Release Photos: ";
				$areaIDField = ($type == 'av') ? "av_area_id" : "photo_area_id";
				if ($type == 'av') {
					$allowed_extensions = MEDIA_EXTENSIONS_ALLOWED_AV;
					$display = "list";
				} else {
					$allowed_extensions = MEDIA_EXTENSIONS_ALLOWED_PHOTO;
					$display = "grid";
				}
				
				$ma = MediaArea::add(0, $areaTitle . $this->title, null, $allowed_extensions, $display);
				if (db::isError($ma)) {
					return $ma;
				}
				
				$ma->activate();
				$q = "update Releases set {$areaIDField} = " . $ma->getID() . " where ID = {$this->ID}";
				$r = mysql_query($q);
				if (!$r) {
					return Error::MySQL();
				} else {
					return $r;
				}
			} else {
				return Error::create("A release with an ID of {$this->ID} is already registered.");
			}
		} else {
			return Error::create('Only an admin user may authorize a release for media.');
		}
	}
	
	function validateMediaOperation($opType = "ADD", $mediaObject = null) {
		// called automatically by the m2 functions that add media, when passed the object
		// opType allows us to distinguish between adding, editing, updating, etc... but most times
		// the same check will work for all operation types
		if ($opType == "RESCAN") {
			// non-admins can't delete, only deactivate
			if (!User::isAdmin()) {
				return Error::create('Only an admin user may rescan media.');
			} else {
				return true;
			}
		} else {
			if (!User::isAdmin()) {
				return Error::create('Only an admin user may add, delete, activate or deactivate release media.');
			} else {
				return true;
			}
		}
	}
}
?>